import { createSlice, PayloadAction, createAsyncThunk } from '@reduxjs/toolkit'
import axios from 'axios'

interface ShoppingCarttate {
  loading: boolean
  error: string | null
  items: any[]
}

const initialState: ShoppingCarttate = {
  loading: true,
  error: null,
  items: [],
}

export const getShoppingCart: any = createAsyncThunk(
  'shoppingCart/getShoppingCart',
  async (token: string, thunkAPI) => {
    const { data } = await axios.get(
      ` https://mock.mengxuegu.com/mock/64fc8254e70b8004a69ea29a/https:/mock.TrvalData/shoppingCart`
      // {
      //   headers: {
      //     Authorization: `bearer ${token}`,
      //   },
      // }
    )
    return data.shoppingCartItems
  }
)

export const addShoppingCartItem: any = createAsyncThunk(
  'shoppingCart/addShoppingCartItem',
  async (parameters: { jwt: string; touristRouteId: string }, thunkAPI) => {
    const { data } = await axios.get(
      ` https://mock.mengxuegu.com/mock/64fc8254e70b8004a69ea29a/https:/mock.TrvalData/shoppingCart/2`
      // {
      //   touristRouteId: parameters.touristRouteId,
      // },
      // {
      //   headers: {
      //     Authorization: `bearer ${parameters.jwt}`,
      //   },
      // }
    )
    return data.shoppingCartItems
  }
)

export const clearShoppingCartItem: any = createAsyncThunk(
  'shoppingCart/clearShoppingCartItem',
  async (parameters: { jwt: string; itemIds: number[] }, thunkAPI) => {
    return await axios.get(
      ' https://mock.mengxuegu.com/mock/64fc8254e70b8004a69ea29a/https:/mock.TrvalData/shoppingCart/2'
      // {
      //   headers: {
      //     Authorization: `bearer ${parameters.jwt}`,
      //   },
      // }
    )
  }
)

export const checkout: any = createAsyncThunk(
  'shoppingCart/checkout',
  async (parameters: { token: string }, thunkAPI) => {
    const { data } = await axios.get(
      ' https://mock.mengxuegu.com/mock/64fc8254e70b8004a69ea29a/https:/mock.TrvalData/pedding'
      // {
      //   headers: {
      //     Authorization: `bearer ${parameters.jwt}`,
      //   },
      // }
    )
    return data
  }
)

export const shoppingCartSlice = createSlice({
  name: 'shoppingCart',
  initialState,
  reducers: {},
  extraReducers: {
    [getShoppingCart.pending.type]: (state) => {
      state.loading = true
    },
    [getShoppingCart.fulfilled.type]: (state: any, action: any) => {
      state.items = action.payload
      state.loading = false
      state.error = null
    },
    [getShoppingCart.rejected.type]: (state, action: any) => {
      state.loading = false
      state.error = action.payload
    },
    [addShoppingCartItem.pending.type]: (state, action: any) => {
      state.loading = true
    },
    [addShoppingCartItem.fulfilled.type]: (state, action) => {
      state.items = action.payload
      state.loading = false
      state.error = null
    },
    [addShoppingCartItem.rejected.type]: (
      state,
      action: PayloadAction<string | null>
    ) => {
      state.loading = false
      state.error = action.payload
    },
    [clearShoppingCartItem.pending.type]: (state) => {
      state.loading = true
    },
    [clearShoppingCartItem.fulfilled.type]: (state) => {
      state.items = []
      state.loading = false
      state.error = null
    },
    [clearShoppingCartItem.rejected.type]: (
      state,
      action: PayloadAction<string | null>
    ) => {
      state.loading = false
      state.error = action.payload
    },
    [checkout.pending.type]: (state) => {
      state.loading = true
    },
    [checkout.fulfilled.type]: (state, action) => {
      state.items = []
      state.loading = false
      state.error = null
    },
    [checkout.rejected.type]: (state, action: PayloadAction<string | null>) => {
      state.loading = false
      state.error = action.payload
    },
  },
})
